#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Copyright (C) 2004-2006 Vittorio Palmisano <vpalmisano at gmail dot com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
import sys
import os

import gtk
import gobject

class SimpleTreeView:
    def __init__(self, treeview, columns):
        self._columns = columns
        _val = [i[1] for i in self._columns]
        self.store = gtk.ListStore(*_val)
        self.treeview = treeview
        self.treeview.set_model(self.store)
        self.add_columns()
        self.treeview.show()
        
    def add_columns(self):
        _index = 0
        for (_name, _type, _clickable, _cb) in self._columns:
            if _type is gobject.TYPE_STRING:
                _rend = gtk.CellRendererText()
                _column = gtk.TreeViewColumn(_name, _rend, text=_index)
                if _clickable:
                    _column.set_clickable(True)
                    self.treeview.set_enable_search(True)
                    _column.set_sort_column_id(_index)
                if _cb:
                    _rend.set_property('editable', True)
                    _rend.connect('edited', _cb, _index)
            elif _type is gobject.TYPE_BOOLEAN:
                _rend = gtk.CellRendererToggle()
                _column = gtk.TreeViewColumn(_name, _rend, active=_index)
                _column.set_clickable(_clickable)
                if _cb:
                    _rend.connect('toggled', _cb, _index)
            _column.set_resizable(True) 
            self.treeview.append_column(_column)
            _index += 1

        self.treeview.columns_autosize()
   
    # example signals
    def cell_text(self, cell, path, new_text, index):
        _iter = self.store.get_iter_from_string(path)
        self.store.set(_iter, index, new_text)
        
    def fixed_toggled(self, cell, path, index):
        _iter = self.store.get_iter_from_string(path)
        _fixed = self.store.get_value(_iter, index)
        self.store.set(_iter, index, not _fixed)
    
    # methods
    def __getitem__(self, index):
        return self.store[(index,)]

    def append(self, parent, *row):
        if isinstance(self.store, gtk.ListStore):
            return self.store.append(*row)
        else:
            return self.store.append(parent, row)
    
    def insert(self, index, row):
        self.store.insert(index, row)

    def remove(self, index):
        del self.store[(index,)]

    def clear(self):
        return self.store.clear()

    ######
    def _make_list(self, model, path, iter, toggle_pos):
        if(model.get_value(iter, toggle_pos) != gtk.FALSE):
            self._list.append(model.get_value(iter, 1))

    def get_list(self):
        self._list = []
        self.store.foreach(self._make_list, 0)
        return self._list

    def get_selected(self):
        selection = self.treeview.get_selection()
        index = selection.get_selected_rows()[1]
        if index:
            return index[0]
        else:
            return None

if __name__ == '__main__':
    w = gtk.Window()
    w.set_default_size(500, 150)
    w.connect('delete-event', gtk.main_quit)
    sc = gtk.ScrolledWindow()
    tw = gtk.TreeView()
    w.add(sc)
    sc.add(tw)
    w.show_all()
    
    #######
    col = [
        ('col1', gobject.TYPE_STRING, True, None),
        ('col2', gobject.TYPE_STRING, True, None),
        ('col3', gobject.TYPE_STRING, True, None),
    ]
    tree = SimpleTreeView(tw, col)

    for i in range(10):
        tree.append(None, ['riga %d' %i, 'blaasdasfasfas', 'blajkjashd'])

    for c in tree[3]:
        print c

    tree.remove(3)
    tree.insert(5, ['bla', '', 'test'])
    tree[5][1] = 'ciao'

    print len(tree.store)

    tw.scroll_to_cell((9,))

    #######
    gtk.main()
